home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / SECDR13A.ZIP / CRYPTDSK.C < prev    next >
C/C++ Source or Header  |  1994-01-29  |  16KB  |  557 lines

  1. /* Secure Drive CRYPTDSK V1.3 */
  2. /* Encrypts/decrypts disks */
  3.  
  4. #include "secdrv.h"
  5.  
  6. extern char pass1[MAXPASS];
  7. extern char pass2[MAXPASS];
  8. extern char Old13Check[4];
  9.  
  10. extern int  tsr_not_installed;
  11. extern int  tsr_wrong_version;
  12. extern int  df10,ef10;
  13. extern char compat_mode;
  14.  
  15. void readseca(unsigned drive,unsigned head,unsigned cyl,unsigned sector,
  16.      unsigned nsects,unsigned secsize,unsigned char *buffer);
  17. void writeseca(unsigned drive,unsigned head,unsigned cyl,unsigned sector,
  18.      unsigned nsects,unsigned secsize,unsigned char *buffer);
  19. void freebuf(void);
  20. void clrbufs(void);
  21.  
  22. unsigned char *buf;
  23. char *pgpv;
  24.  
  25. int main()
  26. {
  27. unsigned drive,firstcyl,firsthead,encrypt,decrypt;            /*1.1*/
  28. unsigned cyl,head,sector,maxcyl,maxhead,maxsector,secsize,t;
  29. unsigned serial[2];
  30. unsigned char dkey[16],dcheck[4],iv[8],tiv[8];                /*1.1*/
  31. unsigned char ekey[16],echeck[4];                             /*1.1*/
  32. unsigned char *bufptr,*dummy;
  33. unsigned i,hdx;
  34. int sethdsafe=0;
  35. int dkeyexp=0;
  36. int ekeyexp=0;
  37.  
  38. char drvltr,r;                                                /*1.1*/
  39. word16 dexpkey[52],eexpkey[52];                               /*1.1*/
  40.  
  41. clrscr();
  42.  
  43. cryptdata=gettsradr();
  44.  
  45. if(tsr_wrong_version)
  46.  {
  47.   printf ("ERROR: Wrong Version (not 1.3A) of SECTSR is Installed\n");
  48.   exit(1);
  49.  }
  50.  
  51. if (tsr_not_installed)
  52.   printf("Warning: SECTSR not installed.\n");
  53.  
  54. if(!(buf=malloc(512))) {
  55.     printf("Error: unable to allocate memory for sector buffer.\n");
  56.     exit(1); }
  57.  
  58. atexit(freebuf);
  59.  
  60. printf("\
  61. Secure Drive CryptDisk Version 1.3A\n\
  62. \n\
  63. This program will encrypt or decrypt a floppy disk or hard drive\
  64.  partition.\n\
  65. \n");
  66.  
  67. pgpv=getenv("PGPPASS");
  68. set_compat_mode();
  69.  
  70.  
  71. askdrive:
  72. printf("Enter the letter of the drive to process, or X to enter the\n\
  73. drive, cylinder, and head manually, or Z to cancel: ");
  74. while(!isalpha(drvltr=toupper(getch())));
  75. printf("%c\n",drvltr);
  76.  
  77.  
  78. if(drvltr=='A') { firstcyl=0; firsthead=0; drive=0; }
  79. else if(drvltr=='B') { firstcyl=0; firsthead=0; drive=1; }
  80.  
  81. else if(drvltr=='X') {
  82.     printf("\nEnter physical drive (0-1), cylinder, and head for the\
  83.  beginning\n\(boot sector) of this partition: ");
  84.     scanf("%u,%u,%u",&drive,&firstcyl,&firsthead);
  85.     drive+=0x80;
  86.     }
  87.  
  88. else if(drvltr=='Z') { printf("\n"); exit(0); }
  89.  
  90. else {
  91.     drive=255;
  92.     readptbl(drvltr,&drive,&firsthead,&firstcyl);  /*1.1*/
  93.     if(drive==255) {
  94.         printf("\nDrive not found.\n\n");
  95.         goto askdrive; }
  96.     printf("\nDrive %c is physical hard drive %u, head %u,\
  97.  cylinder %u\n\n",drvltr,drive,firsthead,firstcyl);
  98.     drive+=0x80;
  99.     }
  100.  
  101. if(drive<0x80) {
  102.    printf("\nInsert disk in drive %c and press any key to\
  103.  continue ",drvltr);
  104.    getch();
  105.    printf("\n\n"); }
  106.  
  107. readsec(drive,firsthead,firstcyl,1,1,buf);
  108. if((buf[510]!=0x55)||(buf[511]!=0xaa)) {
  109.     printf("This is not a boot sector.\n\n");
  110.     exit(1); }
  111.  
  112. encrypt=memcmp(buf+3,"CRYP",4);
  113. decrypt=!encrypt;                       /*1.1*/
  114.  
  115. calcdiskparams(buf,&maxcyl,&maxhead,&maxsector,
  116.                &secsize,serial);
  117.  
  118. printf("This disk has \
  119. %u cylinders, %u sectors, %u heads, sector size %u bytes\n\n",
  120.        maxcyl+1,maxsector,maxhead,secsize);
  121.  
  122. if((buf=realloc(buf,maxsector*secsize)) != NULL)
  123.     printf("Allocated %u bytes for track buffer\n\n",
  124.             maxsector*secsize);
  125. else {
  126.     printf("Error: unable to allocate %u bytes for track buffer\n",
  127.             maxsector*secsize);
  128.     exit(1); }
  129.  
  130. if(encrypt)
  131.     {
  132.     int needkey=1;
  133.     printf("This disk is not encrypted. Do you want to encrypt it? ");
  134.     if(!getyn())
  135.         {
  136.         printf("\n");
  137.         exit(0);
  138.         }
  139.  
  140.     if (pgpv != NULL)
  141.      {
  142.       printf("\nUse PGPPASS as passphrase? ");
  143.       if (getyn())
  144.        {
  145.         strcpy(pass1,pgpv);
  146.         setkeye(ekey,echeck);
  147.         printf("\nPGPPASS entered as %s passphrase\n",
  148.                df10 ? "(V 1.0)":"(V 1.1)");
  149.         clrbufs();
  150.         needkey=0;
  151.        }
  152.      }
  153.  
  154.     if (needkey && cryptdata != NULL && drive<0x80)
  155.      {
  156.       if (memcmp(cryptdata->fkeychk,"\xff\xff\xff\xff",4) &&
  157.          (compat_mode != 'N') == (cryptdata->fkeyv10 != 0))
  158.          /*key taken from SECTSR for encryption must match SD10CMP*/
  159.        {
  160.         printf("Use Floppy Key already in SECTSR? ");
  161.         if (getyn())
  162.          {
  163.           memcpy(echeck,cryptdata->fkeychk,4);
  164.           memcpy(eexpkey,cryptdata->fkey,104);
  165.           df10=cryptdata->fkeyv10;
  166.           ekeyexp=1;
  167.           needkey=0;
  168.          }
  169.        }
  170.      }
  171.  
  172.     if (needkey && cryptdata != NULL && drive<0x80)
  173.      {
  174.       if (memcmp(cryptdata->hkeychk,"\xff\xff\xff\xff",4) &&
  175.          (compat_mode != 'N') == (cryptdata->hkeyv10 != 0))
  176.          /*key taken from SECTSR for encryption must match SD10CMP*/
  177.        {
  178.         printf("Use Hard Disk key already in SECTSR? ");
  179.         if (getyn())
  180.          {
  181.           memcpy(echeck,cryptdata->hkeychk,4);
  182.           memcpy(eexpkey,cryptdata->hkey,104);
  183.           df10=cryptdata->hkeyv10;
  184.           ekeyexp=1;
  185.           needkey=0;
  186.          }
  187.        }
  188.      }
  189.  
  190.     if (needkey)
  191.      {
  192.       getkey(ekey,echeck,TRUE);
  193.       clrbufs();
  194.      }
  195.     }
  196. else
  197.     {
  198.      int needdkey=1;
  199.      printf("This disk is encrypted.\n\
  200. Select (C)hange passphrase, (D)ecrypt, (E)xit: ");           /*1.1*/
  201.      do r=toupper(getch()); while(r!='C'&&r!='D'&&r!='E');   /*1.1*/
  202.      printf("%c\n",r);                                       /*1.1*/
  203.      if(r=='E')                                              /*1.1*/
  204.       {
  205.        printf("\n");
  206.        exit(0);
  207.       }
  208.  
  209.      if (cryptdata != NULL && drive<0x80 &&
  210.          memcmp(cryptdata->fkeychk,buf+7,4) == 0 )
  211.       {
  212.        printf("Floppy Key already in SECTSR will decrypt this diskette.\n");
  213.        memcpy(dcheck,cryptdata->fkeychk,sizeof(dcheck));
  214.        memcpy(dexpkey,cryptdata->fkey,104);
  215.        df10=cryptdata->fkeyv10;
  216.        needdkey=0;
  217.        dkeyexp=1;
  218.       }
  219.  
  220.      if (needdkey && pgpv != NULL)
  221.       {
  222.        strcpy(pass1,pgpv);
  223.        setkeydf(dkey,dcheck,buf+7);
  224.  
  225.        if(!df10 && !memcmp(Old13Check,buf+7,4))
  226.         {
  227.          printf("Check bytes in Disk %c: Boot Sector need updating from 1.3 to 1.1/1.3A. Proceed? ",
  228.                 drvltr);
  229.          if(getyn())
  230.           {
  231.            memcpy(buf+7,dcheck,4);
  232.            writesec(drive,firsthead,firstcyl,1,1,buf);
  233.           }
  234.         }
  235.  
  236.        if(memcmp(dcheck,buf+7,4))
  237.         {
  238.          printf("\nPGPPASS is wrong passphrase.\n");
  239.         }
  240.        else
  241.         {
  242.          printf("\nPGPPASS entered as ");
  243.          if(r=='C') printf("old ");                 /*1.1*/
  244.          if(df10)
  245.           printf("(V 1.0) ");
  246.          else
  247.           printf("(V 1.1) ");
  248.          printf("passphrase will decrypt this disk.\n");
  249.          needdkey=0;
  250.         }
  251.       }
  252.  
  253.  
  254.      if (needdkey)
  255.       {
  256.        for(t=0;t<3;t++)
  257.         {
  258.          printf("\nEnter ");                        /*1.1*/
  259.          if(r=='C') printf("old ");                 /*1.1*/
  260.          printf("passphrase: ");                    /*1.1*/
  261.          getkeydf(dkey,dcheck,buf+7);               /*1.1*/
  262.          if(!df10 && !memcmp(Old13Check,buf+7,4))
  263.           {
  264.            printf("Check bytes in Disk %c: Boot Sector need updating from 1.3 to 1.1/1.3A. Proceed? ",
  265.                   drvltr);
  266.            if(getyn())
  267.             {
  268.              memcpy(buf+7,dcheck,4);
  269.              writesec(drive,firsthead,firstcyl,1,1,buf);
  270.             }
  271.           }
  272.          clrbufs();
  273.          if(!memcmp(dcheck,buf+7,4)) break;         /*1.1*/
  274.          printf("Wrong passphrase.\n");
  275.          if(t==2) exit(1);                          /*1.1*/
  276.         }
  277.       }
  278.     }
  279.     if(r=='C')                                     /*1.1*/
  280.      {                                             /*1.1*/
  281.       int needekey=1;
  282.       encrypt=TRUE;                                /*1.1*/
  283.       if (pgpv != NULL)
  284.        {
  285.         printf("\nUse PGPPASS as new passphrase? ");
  286.         if (getyn())
  287.          {
  288.           printf("\nPGPPASS entered as new %s passphrase\n",
  289.                   compat_mode == 'Y' ? "(V 1.0)":"(V 1.1)");
  290.           strcpy(pass1,pgpv);
  291.           setkeye(ekey,echeck);
  292.           needekey=0;
  293.          }
  294.        }
  295.  
  296.         if (needekey && cryptdata != NULL && drive<0x80 )
  297.          {
  298.           if (!dkeyexp) en_key_idea((word16 *)dkey,dexpkey);
  299.           if (memcmp(dexpkey,cryptdata->fkey,sizeof(dexpkey)) != 0)
  300.             /*floppy key can be new key if not also old key!*/
  301.            {
  302.             if (memcmp(cryptdata->fkeychk,"\xff\xff\xff\xff",4))
  303.              {
  304.               printf("Use Floppy Key already in SECTSR as new key? ");
  305.               if (getyn())
  306.                {
  307.                 memcpy(echeck,cryptdata->fkeychk,sizeof(echeck));
  308.                 memcpy(eexpkey,cryptdata->fkey,104);
  309.                 needekey=0;
  310.                 ekeyexp=1;
  311.                 ef10=cryptdata->fkeyv10;
  312.                }
  313.              }
  314.            }
  315.          }
  316.  
  317.         if (needekey && cryptdata != NULL && drive<0x80 )
  318.          {
  319.           if (!dkeyexp) en_key_idea((word16 *)dkey,dexpkey);
  320.           if (memcmp(dexpkey,cryptdata->hkey,sizeof(dexpkey)) != 0)
  321.             /*HD key can be new key if not also old key!*/
  322.            {
  323.             if (memcmp(cryptdata->hkeychk,"\xff\xff\xff\xff",4))
  324.              {
  325.               printf("Use Hard Drive Key already in SECTSR as new key? ");
  326.               if (getyn())
  327.                {
  328.                 memcpy(echeck,cryptdata->hkeychk,sizeof(echeck));
  329.                 memcpy(eexpkey,cryptdata->hkey,104);
  330.                 needekey=0;
  331.                 ekeyexp=1;
  332.                 ef10=cryptdata->hkeyv10;
  333.                }
  334.              }
  335.            }
  336.          }
  337.  
  338.         if (needekey)
  339.          {
  340.           printf("\n");                              /*1.1*/
  341.           getkey(ekey,echeck,TRUE);                  /*1.1*/
  342.           clrbufs();
  343.          }
  344.  
  345.       if (memcmp(dkey,ekey,sizeof(dkey)) == 0)
  346.        {
  347.         printf("ERROR: Attempt to (C)hange Passphrase with same Key\n");
  348.         exit(1);
  349.        }
  350.      }
  351.  
  352.    if(cryptdata != NULL  && drive >= 0x80)
  353.     {
  354.      /*find empty/Duplicate HD slot */
  355.      for (i=0;i<MAXDRV;i++)
  356.       {
  357.        if (  cryptdata->hd[i].dddrv    == 0  ||
  358.             (cryptdata->hd[i].dddrv    == drive &&
  359.              cryptdata->hd[i].firstcyl == firstcyl) )
  360.         {
  361.          printf("SECTSR will protect drive during %s.\n",
  362.           decrypt ?
  363.             (encrypt ? "passphrase change" : "decryption")
  364.            : "encryption");
  365.          sethdsafe=1;
  366.          break;
  367.         }
  368.       }
  369.     }
  370.  
  371. printf("\nLast chance to abort. Continue? ");
  372. if(!getyn()) exit(1);
  373. bdos(0x0D, 0, 0);          /* Reset Disk Subsystem - Flush all buffers */
  374. clrscr();
  375. gotoxy(1,8);
  376. if (sethdsafe)
  377.  {
  378.   hdx=i;
  379.   if (cryptdata->hd[i].dddrv == 0)
  380.    {
  381.     cryptdata->hd[i].dddrv=drive;
  382.     cryptdata->hd[i].drvltr=drvltr;
  383.     cryptdata->hd[i].firstcyl=firstcyl;
  384.     cryptdata->hd[i].firsthd=firsthead;
  385.     cryptdata->hd[i].firstsec=1;
  386.     cryptdata->hd[i].lastcyl=firstcyl+maxcyl;
  387.     cryptdata->hd[i].maxsec=maxsector;
  388.     cryptdata->hd[i].maxhd=maxhead;
  389.     cryptdata->hd[i].secsize=secsize;
  390.     cryptdata->hd[i].active=0;
  391.    }
  392.   else if (cryptdata->hd[i].active)
  393.    {
  394.     cryptdata->hd[i].active=0;
  395.     memset(cryptdata->hkey,0xbb,104);
  396.    }
  397.  }
  398.  
  399. if (encrypt && decrypt)
  400.  printf("Changing Old %s Passphrase to New %s Passphrase\n",
  401.          df10 ? "(V 1.0) " : "(V 1.1) ",
  402.          ef10 ? "(V 1.0) " : "(V 1.1) ");
  403. else if (encrypt)
  404.  printf("Encrypting %s",
  405.          ef10 ? "(V 1.0) ":"(V 1.1) ");
  406. else
  407.  printf("Decrypting %s",
  408.          df10 ? "(V 1.0) " : "(V 1.1) ");
  409. printf("disk %c:  %u cyls, %u sectors, %u heads, sector size %u bytes",
  410.        drvltr,maxcyl+1,maxsector,maxhead,secsize);
  411.  
  412. if(encrypt && !ekeyexp) en_key_idea((word16 *)ekey,eexpkey);     /*1.1,1.3*/
  413. if(decrypt && !dkeyexp) en_key_idea((word16 *)dkey,dexpkey);     /*1.1,1.3*/
  414.  
  415. printf("\n");
  416. for(cyl=0;cyl<=maxcyl;cyl++)
  417.  for(head=0;head<maxhead;head++)
  418.   {
  419.         if(cyl==0&&head<firsthead) head=firsthead;
  420.         gotoxy(1,11);
  421.         printf("Cylinder %u, Head %u ",cyl,head);     /*1.1*/
  422.         readseca(drive,head,cyl+firstcyl,1,maxsector,secsize,buf);
  423.         bufptr=buf;
  424.         for(sector=1;sector<=maxsector;sector++)
  425.          {
  426.           if(cyl==0&&head==firsthead&§or==1)
  427.            if(encrypt)
  428.             {
  429.              memcpy(&buf[0x03],"CRYP",4);
  430.              memcpy(&buf[0x07],echeck,4);
  431.             }
  432.            else
  433.             memcpy(&buf[0x03],"MSDOS   ",8);
  434.            else
  435.             {
  436.              t=cyl+firstcyl;
  437.              iv[0]=t%256;
  438.              iv[1]=t/256;
  439.              iv[2]=head;
  440.              iv[3]=sector;
  441.              iv[4]=serial[0]%256;
  442.              iv[5]=serial[0]/256;
  443.              iv[6]=serial[1]%256;
  444.              iv[7]=serial[1]/256;
  445.              if(decrypt)                                 /*1.1*/
  446.               {
  447.                memcpy(tiv,iv,sizeof(iv));
  448.                IdeaCFB(tiv,dexpkey,dummy,dummy,1);
  449.                IdeaCFBx(tiv,dexpkey,bufptr,bufptr,secsize/8+1);
  450.               }
  451.              if(encrypt)                                 /*1.1*/
  452.               {
  453.                memcpy(tiv,iv,sizeof(iv));
  454.                IdeaCFB(tiv,eexpkey,dummy,dummy,1);
  455.                IdeaCFB(tiv,eexpkey,bufptr,bufptr,secsize/8+1);
  456.               }
  457.             }
  458.            bufptr+=secsize;
  459.          }
  460.         writeseca(drive,head,cyl+firstcyl,1,maxsector,secsize,buf);
  461.        }
  462.  
  463.  
  464. for(t=0;t<16;t++) { ekey[t]='\0'; dkey[t]='\0'; }   /*1.1*/
  465. for(t=0;t<52;t++) { eexpkey[t]=0; dexpkey[t]=0; }   /*1.1*/
  466.  
  467. gotoxy(1,12);
  468. printf("\n\nDone.\n");
  469. bdos(0x0D, 0, 0);          /* Reset Disk Subsystem - Flush all buffers */
  470. if (sethdsafe)
  471.  {
  472.   if (encrypt)
  473.    {
  474.     printf("Encrypted hard disk now in safe mode.  Use LOGIN to access.\n");
  475.     if (!decrypt)
  476.      printf("Before re-boot, add appropriate LOGIN /S to AUTOEXEC.BAT.\n");
  477.    }
  478.   else
  479.    {
  480.     cryptdata->hd[hdx].dddrv = 0x07f;
  481.      /*remove decrypted hard disk from disk table.
  482.        note: re-boot required to recover drive table slot*/
  483.     printf("Decrypted disk may now be accessed. Before re-boot,\n\
  484. remove corresponding LOGIN  /S from AUTOEXEC.BAT.\n");
  485.    }
  486.  }
  487. return(0);
  488. }
  489.  
  490. void freebuf(void)
  491. {
  492. free(buf);
  493. }
  494.  
  495. void readseca(unsigned drive,unsigned head,unsigned cyl,unsigned sector,
  496.      unsigned nsects,unsigned secsize,unsigned char *buffer)
  497. {
  498. unsigned i,j;
  499. char c;
  500. unsigned r;
  501. for(i=0;i<3;i++)
  502.  {
  503.   r=rldbios(2,drive,head,cyl,sector,nsects,buffer);
  504.   if (!r) return;
  505.  }
  506. gotoxy(1,13);
  507. printf("\nRead error (%02X): drive %02x, head %u, cyl %u\n",
  508.        r,drive,head,cyl);
  509. printf("Reading one sector at a time.\n");
  510. for(j=0;j<nsects;j++) {
  511.   for(i=0;i<3;i++)
  512.    {
  513.     r=rldbios(2,drive,head,cyl,sector+j,1,buffer);
  514.     if(!r) goto goodsec;
  515.    }
  516.   gotoxy(1,15);
  517.   printf("Bad sector (%02X): drive %02x, head %u, cyl %u, sector %u\n",
  518.          r,drive,head,cyl,sector+j);
  519.   goodsec:
  520.   buffer+=secsize;
  521.   }
  522. }
  523.  
  524. void writeseca(unsigned drive,unsigned head,unsigned cyl,unsigned sector,
  525.      unsigned nsects,unsigned secsize,unsigned char *buffer)
  526. {
  527. unsigned i,j;
  528. char c;
  529. unsigned r;
  530. for(i=0;i<3;i++)
  531.  {
  532.   r=rldbios(3,drive,head,cyl,sector,nsects,buffer);
  533.   if(!r) return;
  534.  }
  535. gotoxy(1,13);
  536. if (r == 03 && drive<0x80)
  537.  {
  538.   printf("\nERROR: Disk %c is Write-Protected!\n",drive+'A');
  539.   exit(1);
  540.  }
  541. printf("\nWrite error (%02X): drive %02x, head %u, cyl %u\n",
  542.        r,drive,head,cyl);
  543. printf("Writing one sector at a time.\n");
  544. for(j=0;j<nsects;j++) {
  545.   for(i=0;i<3;i++)
  546.    {
  547.     r=rldbios(3,drive,head,cyl,sector+j,1,buffer);
  548.     if(!r) goto goodsec;
  549.    }
  550.   gotoxy(1,15);
  551.   printf("Bad sector (%02X): drive %02x, head %u, cyl %u, sector %u\n",
  552.          r,drive,head,cyl,sector+j);
  553.   goodsec:
  554.   buffer+=secsize;
  555.   }
  556. }
  557.